package WaitNotify;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NotifyAndNotifyAll {

    public static void main(String[] args) throws InterruptedException {
        Object object = new Object();
        new Thread(() -> {
            log.debug("执行....");
            synchronized (object) {
                try {
                    //object.wait()是无限制的等待
                    //object.wait();

                    //有传参的表示是等待这一段时间，超出时间继续执行;也可以在超时前被提前唤醒
                    object.wait(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            log.debug("执行其他程序 。。。。");
        }, "t1").start();

        new Thread(() -> {
            log.debug("执行....");
            synchronized (object) {
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            log.debug("执行其他程序 。。。。");
        }, "t2").start();

        Thread.sleep(2000);
        synchronized (object) {
            //随机唤醒一个线程
            //object.notify();

            //唤醒全部线程
            object.notifyAll();

            log.debug("主线程 ====>执行唤醒");
        }
    }
}
